﻿@using Nop.Services.Blogs
@using SDF.Core.Infrastructure
@model SDFAuth.Models.PcdModel

@*<script src="@Url.Content("~/Scripts/jquery-1.7.1.intellisense.js")" type="text/javascript"></script>*@
<script>
    $(pageinit);

    function pageinit() {
        var selects = GetSelects();

        selects.change(selectChange);
    }

    function selectChange(e) {
        var selects = GetSelects();
        
        var target = $(e.target);
        
        //选择最后一项，不必传递数据
        var index = selects.index(target);
        if (index == selects.length - 1) {
            return;
        }
        
        var url = "@Url.Action("GetBZ", "Pcd")";
        var param = { target: target.attr("Tag"), id : target.val() };
        $.getJSON(url, param, function (data) {

            $.each(data, function (i, item) {
                FillSelect(selects.eq(index + 1 + i), item);
            });
            
        });     
    

    }
    
    function GetSelects() {
        var selects = $("#spCity select");
        return selects;
    }
    
    function FillSelect(control, list) {
        var tmpl = "<option value='{0}' {2}>{1}</option>";
        var html = "";
        
        for (var i = 0; i < list.length; i++) {

            var selected = "";
            if (i == 0) {
                selected = "selected";
            }
            var item = list[i];
            html += tmpl.format(item.Value, item.Text, selected);
        }
        control.html(html);
    }
    
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function (m, i, o, n) {
            return args[i];
        });
    }
</script>
<span id="spCity">
    @Html.DropDownListFor(m => m.ProvinceID, Model.Province,new {Tag= "Province"})
    @Html.DropDownListFor(m => m.CityID, Model.City,new {Tag= "City"})
    @Html.DropDownListFor(m => m.DistrictID, Model.District,new {Tag= "District"})
</span>
    
